package com.hongxun.design.activitys;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
import android.view.View;
import android.widget.TextView;

import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.adapters.BuyMenuListAdapter;
import com.hongxun.design.bean.BuyMenuBean;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.MyDecimalFormat;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.ui.dialogstyle.MyDialog;
import com.hongxun.design.ui.ui_listview.refreshlist.SwipeRefreshListView;
import com.hongxun.design.utils.fastjson.FastJsonUtil;
import com.hongxun.design.utils.httputil.HttpStringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zerdoor_pc on 2016/8/5.
 */
public class AuthorizationActivity extends BaseActivity implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {
    TextView back, personal, enterprise, pay_price, pay_button;
    Handler buyMenuListHandler;
    Handler UIHandler;
    int index = 2;
    String pwords = "个人授权";
    String ewords = "企业授权";
    List<BuyMenuBean> beans;
    List<BuyMenuBean> leftbeans = new ArrayList<>();
    List<BuyMenuBean> rightbeans = new ArrayList<>();
    BuyMenuListAdapter adapter;
    SwipeRefreshListView list;
    public String buy_menu_id,people_num;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_authorization);
        findView();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    @Override
    protected void findView() {
        super.findView();
        /**界面初始化**/
        back = (TextView) findViewById(R.id.authorization_back);
        personal = (TextView) findViewById(R.id.personal);
        enterprise = (TextView) findViewById(R.id.enterprise);
        enterprise.setVisibility(View.GONE);
        pay_button = (TextView) findViewById(R.id.pay_button);
        pay_price = (TextView) findViewById(R.id.pay_price);
        personal.setOnClickListener(this);
        enterprise.setOnClickListener(this);

        SetTypeface.set(back, IconSourse.BACK);
        pay_button.setText("确认支付");
        SetTypeface.setAppend(pay_button, IconSourse.RIGHTARROW);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mActivity.onBackPressed();
            }
        });

        /**列表初始化**/
        list = (SwipeRefreshListView) findViewById(R.id.authorization_list);
        list.setOnRefreshListener(this);
        addHandler();//加载数据接收handler
        addUIHandler();//加载ui更新handler
        changeButton(1);
    }

    @Override
    protected void initialization() {
        super.initialization();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.personal://个人
                if (index == 1) return;
                changeButton(1);
                break;
            case R.id.enterprise://企业
                if (index == 2) return;
                changeButton(2);
                break;

        }
    }



    /**支付**/
    public void pay(View v){
        MyDialog.payDialog(this,buy_menu_id,people_num,index+"");
    }


    /**
     * 改变现象卡的选中状态
     */
    private void changeButton(int type) {
        clearStatus();
        index = type;
       if (adapter!=null)adapter.currPosition=0;
        buyMenuList(type);
    }

    private void clearStatus() {
        if (index == 1) {
            //选择企业
            setText(personal, pwords, false);
            setText(enterprise, ewords, true);
        } else {
            //选择个人
            setText(personal, pwords, true);
            setText(enterprise, ewords, false);
        }
    }

    private void setText(TextView view, String title, boolean flag) {
        SpannableString styledText1 = new SpannableString("  " + title);
        styledText1.setSpan(new TextAppearanceSpan(this, R.style.style2), 0, styledText1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        if (flag) {
            SetTypeface.set(view, IconSourse.checked);
            view.append(styledText1);
            view.setTextColor(getResources().getColor(R.color.login_yellow));
            view.setBackgroundResource(R.drawable.circular_white_6);
        } else {
            SetTypeface.set(view, IconSourse.unchecked);
            view.append(styledText1);
            view.setTextColor(getResources().getColor(R.color.white));
            view.setBackgroundResource(R.drawable.circular_transwhite_6);
        }

    }


    /**
     * 数据请求*
     */
    private void buyMenuList(int indes) {
        MyApp.HTTP.buyMenuList(buyMenuListHandler, HttpStringUtil.SUCCESSED, "" + indes);
    }


    private void addHandler() {
        buyMenuListHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case HttpStringUtil.SUCCESSED:
                        if (FastJsonUtil.JsonToCheck((String) msg.obj)) {
                            if (beans == null) {
                                beans = new ArrayList<>();
                            } else {
                                beans.clear();
                            }
                            beans.addAll(FastJsonUtil.getBuyMenuListBean((String) msg.obj));
                            buyMenuListHandler.sendEmptyMessage(1);
                        } else {
                            list.setRefreshing(false);
                            ToastShow.showShort(mActivity, FastJsonUtil.JsonToCheck_String((String) msg.obj));
                        }
                        break;
                    case 1:
                        list.setRefreshing(false);
                        if (beans == null) beans = new ArrayList<>();

                        if (beans.size() > 0) {
                            switch (index) {
                                case 1:
                                    leftbeans.clear();
                                    leftbeans.addAll(beans);
                                    break;
                                case 2:
                                    rightbeans.clear();
                                    rightbeans.addAll(beans);
                                    break;
                            }
                        }

                        if (adapter == null) {
                            adapter = new BuyMenuListAdapter(mActivity, beans, UIHandler);
                            adapter.setType(index);
                            list.setAdapter(adapter);
                        } else {
                            adapter.setType(index);
                            adapter.setMap();
                            list.update();
                        }
                        break;
                    case HttpStringUtil.ERROR:
                        list.setRefreshing(false);
                        if (beans != null) {
                            beans.clear();
                            switch (index) {
                                case 1:
                                    beans.addAll(leftbeans);
                                    break;
                                case 2:
                                    beans.addAll(rightbeans);
                                    break;
                            }
                        }
                        buyMenuListHandler.sendEmptyMessage(1);
                        ToastShow.showShort(mActivity, (String) msg.obj);
                        break;
                }
            }
        };
    }


    private void addUIHandler() {
        UIHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case 1:
                        BuyMenuBean bean = null;
                        if (adapter != null) {
                            bean = adapter.getBean();
                        }
                        if (bean == null) return;
                        double price = bean.getProducts_price() + (bean.getPer_averages() * bean.getAddNum());
                        buy_menu_id=bean.getBuy_menu_id()+"";
                        people_num=bean.getAddNum()+"";
                        pay_price.setText("￥" + MyDecimalFormat.getDouble(price,"0.00"));
                        break;
                }
            }
        };
    }

    @Override
    public void onRefresh() {
        buyMenuList(index);
    }
}
